﻿Imports ATS.Business.Entities
Imports ATS.UI.Process
Imports System.ComponentModel
Imports System.Collections.Specialized
Imports ATS.Business.Entities.EntityStateClass

Public Class AccountPolicies
    Private _upc As SubmitterUserProcess = Nothing
    Public accountPolicyValues As admPasswordConfig = Nothing
    Public WithEvents oWindowList As OpenedWindowList = OpenedWindowList.GetList()
    Private WithEvents oWindow As OpenedWindowClass

#Region "Window Events"
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()


        ' Add any initialization after the InitializeComponent() call.
        _upc = New SubmitterUserProcess()
    End Sub

    Private Sub AccountPolicies_Closed(sender As Object, e As System.EventArgs) Handles Me.Closed
        Dim ied As IEnumerable(Of OpenedWindowClass) = From d In oWindowList Where d.WindowID = Me.Name
        oWindowList.Remove(ied.First)
        If Application.Current.Windows.Count = 1 Then
            Application.Current.Windows(0).Activate()
        End If
    End Sub

    Private Sub AccountPolicies_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        If accountPolicyValues.IsDirty Then
            If MessageBox.Show("There are unsaved entries. Do you really want to exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub AccountPolicies_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        loadValuesAccountPolicies()
        HookupChangeEventHandler(accountPolicyValues)
    End Sub
#End Region

#Region "Support Procedures"
    Private Sub SetWindowProperties(dirty As Boolean)
        If dirty Then
            Me.Title &= IIf(Not Me.Title.ToUpper.Contains("UNSAVED"), " (unsaved)", "")
        Else
            Me.Title = IIf(Title.ToUpper.Contains("UNSAVED"), Me.Title.Substring(0, Me.Title.Length - 10), Me.Title)
        End If

        For itemIndex = 0 To oWindowList.Count - 1
            If oWindowList(itemIndex).WindowID = Me.Name Then
                oWindowList(itemIndex).WindowName = Me.Title
                oWindowList(itemIndex).WindowIsDirty = dirty
            End If
        Next

    End Sub
    Private Sub HookupChangeEventHandler(ByVal adminPassConfig As admPasswordConfig)
        ' Add a PropertyChanged event handler for 
        ' the specified Customer instance:
        AddHandler accountPolicyValues.PropertyChanged, _
          AddressOf HandlePropertyChanged
    End Sub

    Private Sub HandlePropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
        If accountPolicyValues.IsDirty Then
            SetWindowProperties(accountPolicyValues.IsDirty)
        End If
    End Sub

    Sub loadValuesAccountPolicies()
        Try
            accountPolicyValues = _upc.AccountPolicyValues
            accountPolicyValues.ResetDirtyBit()
            Me.DataContext = accountPolicyValues



        Catch ex As Exception
            HelperClass.WriteToEventLog(ex.Message.ToString & ControlChars.Lf & ex.ToString, "ATS", EventLogEntryType.Error, "ATS")
            MessageBox.Show(ex.Message.ToString & ControlChars.Lf & ex.ToString, "ATS Error", MessageBoxButton.OK, MessageBoxImage.Error)
        End Try
    End Sub

    Private Sub TextBox_Error(sender As Object, e As ValidationErrorEventArgs)
        If e.Action = ValidationErrorEventAction.Added Then
            DirectCast(sender, Control).ToolTip = e.[Error].ErrorContent.ToString()
        Else
            DirectCast(sender, Control).ToolTip = ""
        End If
    End Sub


#End Region

#Region "Control Events"
    Private Sub buttonRefresh_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles buttonRefresh.Click

        If accountPolicyValues.IsDirty Then
            If MessageBox.Show("There are unsaved entries. Do you really want refresh the data?", "Refresh", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
                loadValuesAccountPolicies()
            End If
        Else
            loadValuesAccountPolicies()
        End If
    End Sub

    Private Sub buttonSave_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles buttonSave.Click
        If accountPolicyValues.IsDirty Then
            _upc.UpdateAccountPolicy(accountPolicyValues)
            MessageBox.Show("Data has been saved successfully.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
            accountPolicyValues.ResetDirtyBit()
            SetWindowProperties(accountPolicyValues.IsDirty)
        Else
            MessageBox.Show("Nothing saved, values were not changed.", "ATS", MessageBoxButton.OK, MessageBoxImage.Information)
        End If
    End Sub
#End Region


End Class


